package cn.gavin.web.controller;

import cn.gavin.domain.Department;
import cn.gavin.qo.QueryObject;
import cn.gavin.service.IDepartmentService;
import cn.gavin.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequiredPermission(name = "部门页面",expression = "department:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        model.addAttribute("pageInfo",departmentService.query(qo));
        return "department/list";
    }

    @RequiredPermission(name = "部门删除",expression = "department:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            departmentService.deleteById(id);
        }
        return "redirect:/department/list";
    }

/*
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            Department department = departmentService.findById(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }
*/

    @RequiredPermission(name = "部门新增/编辑",expression = "department:save")
    @RequestMapping("/save")
    public String save(Department department){
        if (department.getId() != null) {
            departmentService.update(department);
        }else {
            departmentService.insert(department);
        }
        return "redirect:/department/list";
    }
}
